package cn.imvc.app.controllers.seller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import cn.imvc.app.controllers.goods.GoodsControllerImpl;
import cn.imvc.app.entities.Seller;
import cn.imvc.app.entities.Storage;
import cn.imvc.app.entities.StorageType;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.app.models.SellerModel;
import cn.imvc.app.models.StorageModel;
import cn.imvc.app.models.StorageTypeModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;
/**
 * 
 * @author zhy
 * @date 2016年8月1日上午10:07:35
 *
 */
public class StorageControllerImpl implements StorageController{
	static Logger logger = Logger.getLogger(StorageControllerImpl.class);
	public Page<Storage> page;
	public ViewRequest viewRequest;
	public String search_content;
	public Storage storage;
	private int id;
	private int[] ids;
	public int sellerId;
	private int storageTypeId;
	public Seller seller;
	public StorageType storageType;
    public List<Seller> sellers;
    public List<StorageType> storageTypes;
    
	@Autowired
	private StorageModel storageModel;
	@Autowired
	private SellerModel sellerModel;
	@Autowired
	private StorageTypeModel storageTypeModel;
	@Autowired
	LoggingModel loggingModel;
	public String search() {
		
		return null;
	}

	public String index() {
		Map<String, String> searchMap = new HashMap<String, String>();
		searchMap.put("sellerId_eq_string", String.valueOf(sellerId));
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.setParams(searchMap);
		if(search_content!=null&&!search_content.equals("")){
			viewRequest.getParams().put("name_like", search_content);
			viewRequest.getParams().put("_and_seller_dot_id_eq_number", ""+sellerId);
		}
		page = storageModel.find(viewRequest);
		return "storage/index";
	}

	public String show() {	
		storage = storageModel.get(id);
		seller  = storage.getSeller();
		storageType = storage.getStorageType();
		return "storage/edit";
	}
    
	public String editNew() {
	    seller  = sellerModel.get(sellerId);
		
		return "storage/edit";
	}

	public String create() {
		storage.setSeller(sellerModel.get(sellerId));
		storage.setStorageType(storageTypeModel.get(storageType.getId()));
		int result = storageModel.save(sellerId,storageTypeId,storage);
		if(result==0){
			return Render.json(new DialogBean("300","仓库编码不能重复","seller_storage","closeCurrent"));
		}
		if(result==-1){
			return Render.json(new DialogBean("300","仓库名称不能重复","seller_storage","closeCurrent"));
		}
		loggingModel.info(logger,"添加仓库："+storage.getName());
		return Render.json(new DialogBean("200","添加成功","seller_storage","closeCurrent"));
	}

	public String edit() {
		
		storage = storageModel.get(id);
	    seller  = storage.getSeller();
	    storageType = storage.getStorageType();
		return "storage/edit";
	}

	public String update() {
		storage.setSeller(sellerModel.get(sellerId));
		storage.setStorageType(storageTypeModel.get(storageType.getId()));
		int result = storageModel.update(id,sellerId,storageTypeId,storage);
		if(result==0){
			return Render.json(new DialogBean("300","仓库编码不能重复","seller_storage","closeCurrent"));
		}
		if(result==-1){
			return Render.json(new DialogBean("300","仓库名称不能重复","seller_storage","closeCurrent"));
		}
		loggingModel.info(logger,"修改仓库："+storage.getName());
		return Render.json(new DialogBean("200","修改成功","seller_storage","closeCurrent"));
	}

	public String destroy() {
		String result = storageModel.delete(id);
		if(result.equals("")){
			loggingModel.info(logger,"删除仓库："+id);
			return Render.json(new DialogBean("200","删除成功","seller_storage"));
		}else{
			return Render.json(new DialogBean("300", result,"seller_storage")); 
		}
		
	}

	public String destroyBatch() {
		storageModel.deleteBatch(ids);
		return Render.json(new DialogBean("200","删除成功","seller_storage"));
	}
/**
 * 外部调用
 * @return
 */
	public String dialog(){
		
		Map<String, String> searchMap = new HashMap<String, String>();
		searchMap.put("sellerId_eq_string", String.valueOf(sellerId));
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.setParams(searchMap);
		if(search_content!=null&&!search_content.equals("")){
			viewRequest.getParams().put("name_like", search_content);
			viewRequest.getParams().put("_and_seller_dot_id_eq_number", ""+sellerId);
		}
		page = storageModel.find(viewRequest);
		return "storage/dialog";
	}
	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public String getSearch_content() {
		return search_content;
	}

	public void setSearch_content(String search_content) {
		this.search_content = search_content;
	}

	public Storage getStorage() {
		return storage;
	}

	public void setStorage(Storage storage) {
		this.storage = storage;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int[] getIds() {
		return ids;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public int getSellerId() {
		return sellerId;
	}

	public void setSellerId(int sellerId) {
		this.sellerId = sellerId;
	}

	public int getStorageTypeId() {
		return storageTypeId;
	}

	public void setStorageTypeId(int storageTypeId) {
		this.storageTypeId = storageTypeId;
	}

	public List<Seller> getSellers() {
		return sellers;
	}

	public void setSellers(List<Seller> sellers) {
		this.sellers = sellers;
	}

	public List<StorageType> getStorageTypes() {
		return storageTypes;
	}

	public void setStorageTypes(List<StorageType> storageTypes) {
		this.storageTypes = storageTypes;
	}

	public Seller getSeller() {
		return seller;
	}

	public void setSeller(Seller seller) {
		this.seller = seller;
	}

	public StorageType getStorageType() {
		return storageType;
	}

	public void setStorageType(StorageType storageType) {
		this.storageType = storageType;
	}

	



	
}
